package com.martin.lib_base.ui.screen

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Close
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil.compose.AsyncImage
import com.martin.lib_base.R
import com.martin.lib_base.bean.common.KeyValueBean
import com.martin.lib_base.ui.theme.ColorTextNormal

@Composable
fun SingleSelectDialog(
    show: Boolean,
    title: String,
    list: List<KeyValueBean>,
    confirmText: String = "确定",
    onDismiss: () -> Unit,
    onConfirm: (KeyValueBean?) -> Unit = {}
) {
    CommonDialog(
        show = show,
        onDismissRequest = onDismiss,
        alignment = Alignment.BottomCenter
    ) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .background(Color.White)
                .heightIn(min = 200.dp, max = 500.dp)
                .padding(15.dp)
        ) {
            Box(modifier = Modifier.fillMaxWidth()) {
                Text(
                    text = title,
                    fontSize = 15.sp,
                    color = ColorTextNormal,
                    fontWeight = FontWeight.Medium,
                    modifier = Modifier.align(Alignment.Center)
                )
                Icon(
                    imageVector = Icons.Default.Close,
                    contentDescription = null,
                    modifier = Modifier
                        .size(20.dp)
                        .clickable {
                            onDismiss()
                        }
                )
            }
            list.forEach {
                SingleSelectItem(bean = it) { bean ->
                    KeyValueBean.handleSingleChecked(list, position = list.indexOf(bean))
                }
            }
            Box(modifier = Modifier.weight(1f))

            CommonButton(text = confirmText, paddingTop = 20.dp) {
                onConfirm(list.findLast { it.checked.value })
            }
        }
    }
}

@Composable
private fun SingleSelectItem(bean: KeyValueBean, onClick: (KeyValueBean) -> Unit) {
    Row(
        verticalAlignment = Alignment.CenterVertically,
        modifier = Modifier
            .fillMaxWidth()
            .height(50.dp)
            .clickable { onClick(bean) }
    ) {
        Text(
            text = bean.key.value,
            fontSize = 15.sp,
            color = ColorTextNormal
        )
        Box(modifier = Modifier.weight(1f))
        AsyncImage(
            model = if (bean.checked.value) R.drawable.img_check_s else R.drawable.img_check_n,
            contentDescription = null,
            modifier = Modifier.size(15.dp)
        )
    }
}